﻿Imports System.Threading

Namespace CMS

    Public Class Publish

        Private Shared Function newTask(ByVal taskid As Long) As Integer
            Console.WriteLine(String.Format("任务{0}开始", taskid))
            Return taskid
        End Function

        Private Shared Function newTaskCallback() As Integer
            'Console.WriteLine(String.Format("任务{0}开始", taskid))

            Return 0
        End Function

        Private Delegate Function taskDelegate(ByVal taskid As Long) As Integer

        Private Function taskCallBack(ByVal asyncResult As IAsyncResult) As Integer
            If (asyncResult.IsCompleted) Then
                Console.WriteLine("任务{0}完成")
            End If
            Return 1
        End Function

        Sub StartTask(ByVal taskid As Long)
            Dim task As taskDelegate = AddressOf newTask
            Dim asyncResult As IAsyncResult = task.BeginInvoke(taskid, AddressOf taskCallBack, Nothing)
            'IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);

            While (Not asyncResult.IsCompleted)
                Console.Write("*")
                Thread.Sleep(100)
            End While

            '由于异步调用已经完成，因此， EndInvoke会立刻返回结果

            Dim result As Integer = task.EndInvoke(asyncResult)
            Console.WriteLine(result)

        End Sub
    End Class

End Namespace

